#include "fuse.h"

void initFuse(Fuse& fuse)
{
  animInit( fuse.anim, "data/anim2.ani" );
  fuse.tex_feu = LoadGLTexture("data/flames.ppm", true);	
  fuse.tex_cylindre = LoadGLTexture("data/metal.ppm", false);
  fuse.tex_cone = LoadGLTexture("data/metal.ppm", false);
  fuse.tex_sphere = LoadGLTexture("data/metal.ppm", false);
}

void draw_fuse(Fuse& fuse)
{
  glScalef(0.1,0.1,0.1);
  glRotatef( 90, 0,0,-1);
 
  glEnable(GL_TEXTURE_2D);
  glBindTexture( GL_TEXTURE_2D, fuse.tex_cylindre );
  glPushMatrix();
  glScalef(1,7,1);
  draw_cylinder();
  glPopMatrix();
  glDisable(GL_TEXTURE_2D);

  glEnable(GL_TEXTURE_2D);
  glBindTexture( GL_TEXTURE_2D, fuse.tex_cone );
  glPushMatrix();
  glScalef(1,2,1);
  glTranslatef(0,3.5,0);
  draw_cone();
  glPopMatrix();
  glDisable(GL_TEXTURE_2D);

  glPushMatrix();
  glScalef(0.5,1.3,0.5);
  glTranslatef(2,0.6,0);
  draw_sphere(fuse.tex_sphere);
  glPopMatrix();

  glPushMatrix();
  glScalef(0.5,1.3,0.5);
  glTranslatef(0,0.6,2);
  draw_sphere(fuse.tex_sphere);
  glPopMatrix();

  glPushMatrix();
  glScalef(0.5,1.3,0.5);
  glTranslatef(-2,0.6,0);
  draw_sphere(fuse.tex_sphere);
  glPopMatrix();

  glPushMatrix();
  glScalef(0.5,1.3,0.5);
  glTranslatef(0,0.6,-2);
  draw_sphere(fuse.tex_sphere);
  glPopMatrix();

  glPushMatrix();
  glScalef(5,5,5);
  glTranslatef(-0.5,-1,0);
  draw_flame(fuse.tex_feu);
  glPopMatrix();

 }
 
void draw_flame(const unsigned int texture)
{
  int te = ((int) (20*temps())*10);
  const int N = 4;

  glEnable(GL_TEXTURE_2D);
  glBindTexture( GL_TEXTURE_2D, texture );
  glBegin(GL_QUADS);

  glTexCoord2f((float)(te % N)/N,0);
  glVertex3f(0,0,0);
  glTexCoord2f((float)((te+1)% N)/N,0);
  glVertex3f(1,0,0);
  glTexCoord2f((float)((te+1)% N)/N,1);
  glVertex3f(1,1,0);
  glTexCoord2f((float)(te % N)/N,1);
  glVertex3f(0,1,0);
  
  glEnd();
  glDisable(GL_TEXTURE_2D);  
}
void anim_fuse(Fuse& fuse)
{
  glPushMatrix();
  float t = 5*temps();
  int te = ((int) (t)) % fuse.anim.nbp;
  float c = t - ((int) (t) );
  int te_suivant = (te+1) % fuse.anim.nbp;
  int te_resuivant = (te_suivant + 1) % fuse.anim.nbp;
  Vec3f pt_Pos,pt_Pos2, pt_Y;
  Vec3f vt_codir, vt_dirf;

  //premier point
  pt_Pos.x = (1 - c) * fuse.anim.P[te].x + c * fuse.anim.P[te_suivant].x;
  pt_Pos.y = (1 - c) * fuse.anim.P[te].y + c * fuse.anim.P[te_suivant].y;
  pt_Pos.z = (1 - c) * fuse.anim.P[te].z + c * fuse.anim.P[te_suivant].z;
  glTranslatef(pt_Pos.x, pt_Pos.y, pt_Pos.z);

  
  //second point
  pt_Pos2.x = (1 - c) * fuse.anim.P[te_suivant].x + c * fuse.anim.P[te_resuivant].x;
  pt_Pos2.y = (1 - c) * fuse.anim.P[te_suivant].y + c * fuse.anim.P[te_resuivant].y;
  pt_Pos2.z = (1 - c) * fuse.anim.P[te_suivant].z + c * fuse.anim.P[te_resuivant].z;
  glTranslatef(pt_Pos2.x, pt_Pos2.y, pt_Pos2.z);

  
  vecSub(vt_dirf,pt_Pos2, pt_Pos);
  vecNormalize(vt_dirf);
  
  vecInit(pt_Y,0,1,0);
  vecCross(vt_codir,vt_dirf,pt_Y);
  float m[16] = { vt_dirf.x, vt_dirf.y, vt_dirf.z, 0,
		  0    ,     1,     0, 0,
		  vt_codir.x,vt_codir.y,vt_codir.z,0,
		  0      ,      0,      0,1};
  glMultMatrixf(m);
  draw_fuse(fuse);
  glPopMatrix();
}
